package com.qfedu.web.servlet;

import com.qfedu.pojo.Cart;
import com.qfedu.pojo.Goods;
import com.qfedu.pojo.User;
import com.qfedu.service.CartService;
import com.qfedu.service.GoodsService;
import com.qfedu.service.impl.CartServiceImpl;
import com.qfedu.service.impl.GoodsServiceImpl;
import com.qfedu.web.common.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 创建者: QF-SUN
 * 创建时间: 2023/4/4 14:46
 * 描述:
 */
@WebServlet("/cartservlet")
public class CartServlet extends BaseServlet {


    CartService cartService = new CartServiceImpl();
    GoodsService goodsService = new GoodsServiceImpl();

    public String getCart(HttpServletRequest request , HttpServletResponse response) throws Exception{
        Object object = request.getSession().getAttribute("user");
        if(object == null){
            return "/login.jsp?error";
        }
        User user = (User) object;

        //查询购物车，且需要包含商品对象
        List<Cart> cartList = cartService.selectCart(user.getId());
        request.setAttribute("carts", cartList);

        return "forward:/cart.jsp";
    }


    public String addCart(HttpServletRequest request , HttpServletResponse response) throws Exception{

        HttpSession session = request.getSession();
        Object user = session.getAttribute("user");
        if(user == null){
            return "redirect:/login.jsp?error";
        }

        User u = (User) user;
        int uid = u.getId(); //5
        String goodsId = request.getParameter("goodsId");  // 6
        String number = request.getParameter("number"); //1

        //根据商品id查询商品对象
        Goods goods = goodsService.findById(Integer.parseInt(goodsId));

        //根据用户id， 根据商品id， 查询购物车对象
        Cart cart = cartService.findCart(uid, goodsId);

        //如果没有购物车对象，则新增
        if(cart == null){
            cart = new Cart();
            cart.setId(uid);
            cart.setPid(Integer.parseInt(goodsId));
            cart.setNum(Integer.parseInt(number));
            cart.setMoney(goods.getPrice() * Integer.parseInt(number));

            //持久化到数据库
            cartService.insert(cart);
        }
        //如果有购物车对象，则修改
        else {
            //修改 数量 和  金额
            cart.setNum(cart.getNum() + Integer.parseInt(number));
            cart.setMoney(cart.getNum() * goods.getPrice());

            cartService.update(cart);
        }

        return "redirect:/cartSuccess.jsp";

    }

    public String addCartAjax(HttpServletRequest request , HttpServletResponse response) throws Exception{

        HttpSession session = request.getSession();
        Object user = session.getAttribute("user");
        if(user == null){
            return "redirect:/login.jsp?error";
        }

        User u = (User) user;
        int uid = u.getId(); //5
        String goodsId = request.getParameter("goodsId");  // 6
        String number = request.getParameter("number"); //1

        //根据商品id查询商品对象
        Goods goods = goodsService.findById(Integer.parseInt(goodsId));

        int num = Integer.parseInt(number);
        //如果num 为0 ，删除该商品对应的购物车
        if(num == 0) {
            cartService.deleteCart(uid,goodsId );
        }else {
            //根据用户id， 根据商品id， 查询购物车对象
            Cart cart = cartService.findCart(uid, goodsId);

            //如果没有购物车对象，则新增
            if(cart == null){
                cart = new Cart();
                cart.setId(uid);
                cart.setPid(Integer.parseInt(goodsId));
                cart.setNum(num);
                cart.setMoney(goods.getPrice() * num);

                //持久化到数据库
                cartService.insert(cart);
            }
            //如果有购物车对象，则修改
            else {
                //修改 数量 和  金额
                cart.setNum(cart.getNum() + num);
                cart.setMoney(cart.getNum() * goods.getPrice());

                cartService.update(cart);
            }

        }
        return "success";

    }


    public String clearCartAjax(HttpServletRequest request , HttpServletResponse response) throws Exception{
        HttpSession session = request.getSession();
        Object user = session.getAttribute("user");
        if(user == null){
            return "redirect:/login.jsp?error";
        }

        User u = (User) user;

        cartService.clearCart(u.getId());

        return "success";
    }
}
